﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Blo;
using Dict.DataModel;

namespace UI
{
    public class EquipTab : MonoBehaviour
    {
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public int curType;
        public ArrayPage arrayPage;
        public GameObject emptyPrefab;
        public List<UIToggle> tabBtns;
        
        EquipIcon curChosedEquip;

        public void SetInnerTab(int type)
        {
            curType = type;
            tabBtns[type].Set(true);
            UpdateTab();
        }

        public void UpdateTab()
        {
            StopCoroutine("UpdateList");
            if (gameObject.activeSelf)
                StartCoroutine("UpdateList");
            //arrayPage.StartCoroutine(UpdateList());
        }

        bool CheckLuck(string id)
        {
            UIHeroFormation formation = StaticData.playerData.InstPlayerData.BattleCamps[arrayPage.CurPosition];
            string heroId = formation.hero.heroId;
            List<TableYuan> luckDatas = Dict.DictDataManager.Instance.tableYuanDao.GetByOneIndex(heroId);

            foreach (var item in luckDatas)
            {
                if (!string.IsNullOrEmpty(item.equip0) && item.equip0.CompareTo(id) == 0)
                    return true;

                if (!string.IsNullOrEmpty(item.treaId0) && item.treaId0.CompareTo(id) == 0)
                    return true;
            }

            return false;
        }

        IEnumerator UpdateList()
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            
            List<UIEquipment> list = StaticData.playerData.InstPlayerData.GetUIEquipmentsByType(curType);
            StaticData.playerData.InstPlayerData.SortEquipListForBag(list);
            List<UITreasure> list1 = StaticData.playerData.InstPlayerData.GetUITreasureByType(curType, false);
            StaticData.playerData.InstPlayerData.SortTreasureListForBag(list1);
            QualityValue[] qualityOrder = { QualityValue.RED, QualityValue.YELLOW, QualityValue.PURPLE, QualityValue.BLUE, QualityValue.GREEN, QualityValue.WHITE };
            int count = 0;
            for(int a = 0; a < qualityOrder.Length; a++)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if(list[i].equipData.quality == (int)qualityOrder[a])
                    {
                        if (list[i].owner != null)
                            continue;

                        GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                        GameObject equipIcon = Instantiate(obj);
                        Destroy(equipIcon.GetComponent<BoxCollider>());
                        equipIcon.GetComponent<EquipIcon>().SetEquip(list[i]);
                        equipIcon.GetComponent<EquipIcon>().SetWithChoseBg(true);
                        equipIcon.GetComponent<EquipIcon>().SetChosedCallback(new EquipIcon.ClickCallBack(this.OnEquipClick));
                        simpleGrid.AddChild(equipIcon);
                        equipIcon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                        count++;
                        equipIcon.name = count.ToString();

                        if (CheckLuck(list[i].equipId))
                            equipIcon.GetComponent<EquipIcon>().imgJiban.SetActive(true);

                        if (count % 40 == 0)
                            yield return 1;
                    }
                }

                for (int i = 0; i < list1.Count; i++)
                {
                    if(list1[i].treasureData.quality == (int)qualityOrder[a])
                    {
                        if (list1[i].owner != null)
                            continue;

                        GameObject obj = AssetLoad.ResourceManager.Instance.LoadResource("prefab/EquipIcon") as GameObject;
                        GameObject equipIcon = Instantiate(obj);
                        Destroy(equipIcon.GetComponent<BoxCollider>());
                        equipIcon.GetComponent<EquipIcon>().SetTreasure(list1[i]);
                        equipIcon.GetComponent<EquipIcon>().SetWithChoseBg(true);
                        equipIcon.GetComponent<EquipIcon>().SetChosedCallback(new EquipIcon.ClickCallBack(this.OnEquipClick));
                        simpleGrid.AddChild(equipIcon);
                        equipIcon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                        count++;
                        equipIcon.name = count.ToString();
                        if (CheckLuck(list1[i].treasureId))
                            equipIcon.GetComponent<EquipIcon>().imgJiban.SetActive(true);
                        if (count % 40 == 0)
                            yield return 1;
                    }
                }
            }

            //不够的地方，补充空白块儿
            int pagecount = 16;
            if (count > pagecount && count % 4 != 0)
                pagecount = count + 4 - count % 4;
            for (int i = count; i < pagecount; i++)
            {
                GameObject icon = Instantiate(emptyPrefab);
                Destroy(icon.GetComponent<BoxCollider>());
                simpleGrid.AddChild(icon);
                icon.transform.localScale = new Vector3(0.9f, 0.9f, 0.9f);
                icon.SetActive(true);
                count++;
            }
            
            scrollView.ResetPosition();
        }

        public void OnEquipClick(EquipIcon obj)
        {
            if(curChosedEquip != null)
            {
                curChosedEquip.chosedBg.SetActive(false);
            }
            curChosedEquip = obj;

            string options = "";
            if (obj.m_equip != null)
                options += "equipGuid=" + obj.m_equip.guid;
            if (obj.m_treasure != null)
                options += "treasureGuid=" + obj.m_treasure.guid;

            options += "&index=" + arrayPage.CurPosition.ToString();
            PageManager.Instance.OpenPage("EquipSimplePage", options);
        }
        
        public void OnTabClick(GameObject obj)
        {
            switch(obj.name)
            {
                case "Weapon":
                    curType = 1;
                    break;
                case "Armor":
                    curType = 2;
                    break;
                case "Arms":
                    curType = 3;
                    break;
                case "Shoes":
                    curType = 4;
                    break;
                case "Ring":
                    curType = 5;
                    break;
                case "Necklace":
                    curType = 6;
                    break;
                default:
                    curType = 0;
                    break;
            }
            StopCoroutine("UpdateList");
            StartCoroutine("UpdateList");
        }
    }
}
